* This dofile creates the figures and tables shown in the Online Appendix.

***** Appendix A *****

// Figure A.1
use "$output/database_win", clear

keep if main_sample_indiv==1

rdplot votesh_indiv_R1_next running, p(2) nbins(20 20) ///
graph_options(ytitle("Cand. vote share, t+1") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$appendix/FigureA1_indiv.png", replace		
				
use "$output/database_win", clear

keep if main_sample_party==1

rdplot votesh_party_R1_next running, p(2) nbins(20 20) ///
graph_options(ytitle("Party vote share, t+1") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$appendix/FigureA1_party.png", replace		

// Table A.1
estimates clear

forval i=0/1{

use "$output/database_win", clear

gen legi=election_type==1
keep if legi==`i'

keep if main_sample_indiv==1

eststo: rdrobust win_indiv_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

forval i=0/1{

use "$output/database_win", clear

keep if gender_cand==`i'

keep if main_sample_indiv==1

eststo: rdrobust win_indiv_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

foreach i in "0" "1"{

use "$output/database_win", clear

keep if left==`i'

keep if main_sample_indiv==1

eststo: rdrobust win_indiv_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

forval i=0/1{

use "$output/database_win", clear

keep if samepres==`i'

keep if main_sample_indiv==1

eststo: rdrobust win_indiv_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

foreach p in "FDG" "SOC" "MDM" "UMP"{

use "$output/database_win", clear

keep if party=="`p'"

keep if main_sample_indiv==1

eststo: rdrobust win_indiv_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

use "$output/database_win", clear

keep if main_sample_indiv==1

sum year, det
gen recent=0 if year<=r(p50)
replace recent=1 if year>r(p50)

eststo: rdrobust win_indiv_next running if recent==0, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust win_indiv_next running if recent==1, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
			
esttab using "$appendix/TableA1.tex", b(3) se nonotes booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("Cand. wins, t+1", pattern(1 0 0 0 0 0 0 0 0 0 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles("Local" "Parliamentary" "Male" "Female" "\shortstack{Left or\\far-left}" "\shortstack{Other\\orientations}"  "Opposition" "Majority" "FDG" "SOC" "MDM" "UMP" "\shortstack{Before\\1994}" "\shortstack{After\\1994}")
// NB: we change the RD_Estimate label and add significance stars based on robust p-value by hand

// Table A.2
estimates clear

forval i=0/1{

use "$output/database_win", clear

gen legi=election_type==1
keep if legi==`i'

keep if main_sample_indiv==1

eststo: rdrobust running_indiv_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

forval i=0/1{

use "$output/database_win", clear

keep if gender_cand==`i'

keep if main_sample_indiv==1

eststo: rdrobust running_indiv_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

foreach i in "0" "1"{

use "$output/database_win", clear

keep if left==`i'

keep if main_sample_indiv==1

eststo: rdrobust running_indiv_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}


forval i=0/1{

use "$output/database_win", clear

keep if samepres==`i'

keep if main_sample_indiv==1

eststo: rdrobust running_indiv_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

foreach p in "FDG" "SOC" "MDM" "UMP"{

use "$output/database_win", clear

keep if party=="`p'"

keep if main_sample_indiv==1

eststo: rdrobust running_indiv_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

use "$output/database_win", clear

keep if main_sample_indiv==1

sum year, det
gen recent=0 if year<=r(p50)
replace recent=1 if year>r(p50)

eststo: rdrobust running_indiv_next running if recent==0, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust running_indiv_next running if recent==1, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
			
esttab using "$appendix/TableA2.tex", b(3) se nonotes booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("Cand. runs, t+1", pattern(1 0 0 0 0 0 0 0 0 0 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles("Local" "Parliamentary" "Male" "Female" "\shortstack{Left or\\far-left}" "\shortstack{Other\\orientations}"  "Opposition" "Majority" "FDG" "SOC" "MDM" "UMP" "\shortstack{Before\\1994}" "\shortstack{After\\1994}")

// Table A.3
estimates clear

use "$output/database_win", clear

keep if main_sample_orient==1 

byso id_unique: gen orientation_opp=orientation[2] if _n==1
byso id_unique: replace orientation_opp=orientation[1] if _n==2

gen nb_cands_neighbor=nb_cands_orient_R1_fl_next+nb_cands_orient_R1_c_next if orientation=="l"
replace nb_cands_neighbor=nb_cands_orient_R1_c_next+nb_cands_orient_R1_fr_next if orientation=="r"
replace nb_cands_neighbor=nb_cands_orient_R1_l_next+nb_cands_orient_R1_r_next if orientation=="c"
replace nb_cands_neighbor=nb_cands_orient_R1_l_next if orientation=="fl"
replace nb_cands_neighbor=nb_cands_orient_R1_r_next if orientation=="fr"

eststo: rdrobust nb_cands_neighbor running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_cands_neighbor if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
estadd local sample "All cand."

keep if (orientation=="l" & (orientation_opp=="r" | orientation_opp=="fr")) | (orientation=="fl" & (orientation_opp=="c" | orientation_opp=="r" | orientation_opp=="fr")) | (orientation=="c" & (orientation_opp=="fl" | orientation_opp=="fr")) | (orientation=="fr" & (orientation_opp=="c" | orientation_opp=="l" | orientation_opp=="fl")) | (orientation=="r" & (orientation_opp=="l" | orientation_opp=="fl"))

eststo: rdrobust nb_cands_neighbor running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_cands_neighbor if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
estadd local sample "One orient. apart"

esttab using "$appendix/TableA3.tex", b(3) se stats(rob_p obs poly band mean_control sample, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold" "Sample")) nonotes nomtitles booktabs nocon nostar nonotes replace	 mgroups("\shortstack{Neighboring Orient. nb. all cand., t+1}", pattern(1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

// Table A.4
estimates clear

use "$output/database_win", clear

keep if main_sample_party==1

eststo: bootstrap upper_bound=e(upper_bound) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("win_party_next") running_level_next("running_party_next") band(0.05)
estadd scalar band=0.05
sum win_party_next if running>=-0.05 & running<0 & running_party_next==1
estadd scalar mean_control=r(mean)

use "$output/database_win", clear

keep if main_sample_party==1

eststo: bootstrap upper_bound=e(upper_bound) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("votesh_party_R1_next") running_level_next("running_party_next") band(0.05)
estadd scalar band=0.05
sum votesh_party_R1_next if running>=-0.05 & running<0 & running_party_next==1
estadd scalar mean_control=r(mean)

use "$output/database_win", clear

keep if main_sample_orient==1
		
eststo: bootstrap upper_bound=e(upper_bound2) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next_alt, outcome_next("nb_othercands_diffparty_R1_next") running_level_next("running_party_next") band(0.05)
estadd scalar band=0.05
sum nb_othercands_diffparty_R1_next if running>=-0.05 & running<0 & running_party_next==1
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableA4.tex", replace b(3) se noobs nomtitles nonotes booktabs star(* 0.10 ** 0.05 *** 0.01) ///
coeflabels(upper_bound "Upper bound" lower_bound "Lower bound") mgroups("Party wins, t+1" "Party vote share, t+1" "\shortstack{Orient. nb. other\\cand., t+1}", pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(N_reps mean_control, fmt(0 3) label( "Bootstrap replications" "Mean, left of threshold"))

// Table A.5
estimates clear

use "$output/database_win", clear

keep if main_sample_party==1 & (coalition=="L" | coalition=="R")

eststo: rdrobust nb_othercands_coal_R1_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_othercands_coal_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

gen any_coal_next=nb_othercands_coal_R1_next>0 if nb_othercands_coal_R1_next!=.

eststo: rdrobust any_coal_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum any_coal_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableA5.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("\shortstack{Coal. nb. other\\cand., t+1}" "\shortstack{Coal. any other\\cand., t+1}", pattern(1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
 
// Table A.6
estimates clear

use "$output/database_win", clear

keep if main_sample_indiv==1
		
sum nb_othercands_orient_R1 if running>-0.05 & running<0.05,d
scalar avg = r(mean)

eststo: rdrobust win_indiv_next running if nb_othercands_orient_R1>=avg, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0 & nb_othercands_orient_R1>=avg
estadd scalar mean_control=r(mean)

use "$output/database_win", clear

keep if main_sample_orient == 1
		
sum nb_othercands_orient_R1 if running>-0.05 & running<0.05,d
scalar avg = r(mean)

eststo: rdrobust nb_othercands_orient_R1_next running if nb_othercands_orient_R1>=avg, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_othercands_orient_R1_next if running>=-e(h_l) & running<0 & nb_othercands_orient_R1>=avg
estadd scalar mean_control=r(mean)

use "$output/database_win", clear

keep if main_sample_indiv==1
		
sum nb_othercands_orient_R1 if running>-0.05 & running<0.05,d
scalar avg = r(mean)

eststo: rdrobust win_indiv_next running if nb_othercands_orient_R1<avg, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0 & nb_othercands_orient_R1<avg
estadd scalar mean_control=r(mean)

use "$output/database_win", clear

keep if main_sample_orient == 1
		
sum nb_othercands_orient_R1 if running>-0.05 & running<0.05,d
scalar avg = r(mean)

eststo: rdrobust nb_othercands_orient_R1_next running if nb_othercands_orient_R1<avg, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_othercands_orient_R1_next if running>=-e(h_l) & running<0 & nb_othercands_orient_R1<avg
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableA6.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace mgroups("\shortstack{Cand. wins, t+1 \\ (more than avg)}"  "\shortstack{Orient. nb. other cand.,\\ t+1 (more than avg)}" "\shortstack{Cand wins., t+1 \\ (fewer than avg)}" "\shortstack{Orient. nb. other cand.,\\t+1 (fewer than avg)}", pattern(1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

// Table A.7
estimates clear

use "$output/database_runoff", clear

keep if main_sample_indiv2 == 1 & assignment_type == 2

foreach y in win_indiv_next running_indiv_next qualif_indiv_next {

eststo: rdrobust `y' running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

use "$output/database_runoff", clear

keep if main_sample_orient2 == 1 & assignment_type == 2

foreach y in nb_othercands_orient_R1_next nb_cands_orient_R1_next{

eststo: rdrobust `y' running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

esttab using "$appendix/TableA7.tex", stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) b(3) se nonotes nomtitles booktabs nocon nostar nonotes replace mgroups("\shortstack{Cand. wins,\\t+1}" "\shortstack{Cand. runs,\\t+1}" "\shortstack{Cand. qualif.,\\t+1}" "\shortstack{Orient nb. other\\cand., t+1}" "\shortstack{Orient nb. all\\cand., t+1}", pattern(1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

// Table A.8
estimates clear

use "$output/database_runoff", clear

keep if main_sample_indiv1==1 & assignment_type==1

foreach y in win_indiv_next running_indiv_next qualif_indiv_next{

eststo: rdrobust `y' running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}
	
use "$output/database_runoff", clear

keep if main_sample_orient1==1 & assignment_type==1

foreach y in nb_othercands_orient_R1_next nb_cands_orient_R1_next{

eststo: rdrobust `y' running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

esttab using "$appendix/TableA8.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("\shortstack{Cand. wins,\\t+1}" "\shortstack{Cand. runs,\\t+1}" "\shortstack{Cand. qualif.,\\t+1}" "\shortstack{Orient nb. other\\cand., t+1}" "\shortstack{Orient nb. all\\cand., t+1}", pattern(1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
 
// Table A.9
estimates clear

use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3

eststo: rdrobust win_indiv running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
estadd local treat="Runoff"

local rd=e(tau_cl)

use "$output/database_win", clear

keep if main_sample_indiv==1

foreach y in win_indiv_next running_indiv_next qualif_indiv_next{

eststo: rdrobust `y' running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
estadd local treat="Winning"
estadd scalar rd_inf=`rd'*e(tau_cl)
}

esttab using "$appendix/TableA9.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control treat rd_inf, fmt(3 0 0 3 3 0 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold" "Treatment" "Pred. treatment effect")) replace ///
 mgroups("\shortstack{Cand wins,\\t}" "\shortstack{Cand wins,\\t+1}" "\shortstack{Cand. runs,\\t+1}" "\shortstack{Cand qualif.,\\t+1}", pattern(1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

// Table A.10
estimates clear

use "$output/database_runoff", clear

keep if main_sample_party3==1 & assignment_type==3

foreach y in win_party_next running_party_next qualif_party_next{

eststo: rdrobust `y' running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

use "$output/database_runoff", clear

keep if main_sample_orient3==1 & assignment_type==3

eststo: rdrobust nb_othercands_diffparty_R1_next running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_othercands_diffparty_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableA10.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("\shortstack{Party wins,\\t+1}" "\shortstack{Party runs,\\t+1}" "\shortstack{Party qualif.,\\t+1}" "\shortstack{Orient. nb. other\\ cand., t+1}", pattern(1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 

// Table A.11
estimates clear

use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3

foreach y in win_indiv_next votesh_indiv_R1_next qualif_indiv_next{

eststo: bootstrap upper_bound=e(upper_bound) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("`y'") running_level_next("running_indiv_next") band(0.025)
sum `y' if running>=-0.025 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)
}

use "$output/database_runoff", clear

keep if main_sample_orient3==1 & assignment_type==3

eststo: bootstrap upper_bound=e(upper_bound2) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next_alt, outcome_next("nb_othercands_orient_R1_next") running_level_next("running_indiv_next") band(0.025)
estadd scalar band=0.025
sum nb_othercands_orient_R1_next if running>=-0.025 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableA11.tex", replace b(3) se noobs nomtitles nonotes booktabs star(* 0.10 ** 0.05 *** 0.01) ///
coeflabels(upper_bound "Upper bound" lower_bound "Lower bound") mgroups("\shortstack{Cand. wins,\\t+1}" "\shortstack{Cand. vote\\share, t+1}" "\shortstack{Cand. qualif.,\\t+1}" "\shortstack{Orient. nb. other \\cand.,t+1}", pattern(1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		stats(N_reps mean_control, fmt(0 3) label( "Bootstrap replications" "Mean, left of threshold"))

// Table A.12
estimates clear

use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3

drop if orientation=="nc"

gen or2 = orientation if ranking_indiv_R1 == 2
gen or3 = orientation if ranking_indiv_R1 == 3
keep if (secround_type == 1 & (or3 == orientation_rank1 | or3 == orientation_rank2)) | (secround_type == 2 & orientation == orientation_rank1)

foreach y in win_indiv_next running_indiv_next qualif_indiv_next{

eststo: rdrobust `y' running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}
	
use "$output/database_runoff", clear

keep if main_sample_orient3==1 & assignment_type==3

gen or2 = orientation if ranking_indiv_R1 == 2
gen or3 = orientation if ranking_indiv_R1 == 3
keep if (secround_type == 1 & (or3 == orientation_rank1 | or3 == orientation_rank2)) | (secround_type == 2 & orientation == orientation_rank1)

foreach y in nb_othercands_orient_R1_next nb_cands_orient_R1_next{

eststo: rdrobust `y' running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}
		
esttab using "$appendix/TableA12a.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("\shortstack{Cand. wins,\\t+1}" "\shortstack{Cand. runs,\\t+1}" "\shortstack{Cand. qualif.,\\t+1}" "\shortstack{Orient nb. other\\cand., t+1}" "\shortstack{Orient nb. all\\cand., t+1}", pattern(1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
 
estimates clear

use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3

drop if orientation=="nc"

gen or2 = orientation if ranking_indiv_R1 == 2
gen or3 = orientation if ranking_indiv_R1 == 3
keep if (secround_type == 1 & or3 != orientation_rank1 & or3 != orientation_rank2) | (secround_type == 2 & orientation != orientation_rank1)

foreach y in win_indiv_next running_indiv_next qualif_indiv_next{

eststo: rdrobust `y' running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}
	
use "$output/database_runoff", clear

keep if main_sample_orient3==1 & assignment_type==3

gen or2 = orientation if ranking_indiv_R1 == 2
gen or3 = orientation if ranking_indiv_R1 == 3
keep if (secround_type == 1 & or3 != orientation_rank1 & or3 != orientation_rank2) | (secround_type == 2 & orientation != orientation_rank1)

foreach y in nb_othercands_orient_R1_next nb_cands_orient_R1_next{

eststo: rdrobust `y' running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

esttab using "$appendix/TableA12b.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("\shortstack{Cand. wins,\\t+1}" "\shortstack{Cand. runs,\\t+1}" "\shortstack{Cand. qualif.,\\t+1}" "\shortstack{Orient nb. other\\cand., t+1}" "\shortstack{Orient nb. all\\cand., t+1}", pattern(1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		
// Table A.13
estimates clear

use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3

drop if orientation=="nc"

gen or2 = orientation if ranking_indiv_R1 == 2
gen or3 = orientation if ranking_indiv_R1 == 3
keep if (secround_type == 1 & (or3 == orientation_rank1 | or3 == orientation_rank2)) | (secround_type == 2 & orientation == orientation_rank1)

foreach y in win_indiv_next votesh_indiv_R1_next qualif_indiv_next{

eststo: bootstrap upper_bound=e(upper_bound) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("`y'") running_level_next("running_indiv_next") band(0.025)
sum `y' if running>=-0.025 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)
}

use "$output/database_runoff", clear

keep if main_sample_orient3==1 & assignment_type==3

gen or2 = orientation if ranking_indiv_R1 == 2
gen or3 = orientation if ranking_indiv_R1 == 3
keep if (secround_type == 1 & (or3 == orientation_rank1 | or3 == orientation_rank2)) | (secround_type == 2 & orientation == orientation_rank1)

eststo: bootstrap upper_bound=e(upper_bound2) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next_alt, outcome_next("nb_othercands_orient_R1_next") running_level_next("running_indiv_next") band(0.025)
estadd scalar band=0.025
sum nb_othercands_orient_R1_next if running>=-0.025 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableA13a.tex", replace b(3) se noobs nomtitles nonotes booktabs star(* 0.10 ** 0.05 *** 0.01) ///
coeflabels(upper_bound "Upper bound" lower_bound "Lower bound") mgroups("\shortstack{Cand. wins,\\t+1}" "\shortstack{Cand. vote\\share, t+1}" "\shortstack{Cand. qualif.,\\t+1}" "\shortstack{Orient. nb. other \\cand.,t+1}", pattern(1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(N_reps mean_control, fmt(0 3) label( "Bootstrap replications" "Mean, left of threshold"))

estimates clear

use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3

drop if orientation=="nc"

gen or2 = orientation if ranking_indiv_R1 == 2
gen or3 = orientation if ranking_indiv_R1 == 3
keep if (secround_type == 1 & or3 != orientation_rank1 & or3 != orientation_rank2) | (secround_type == 2 & orientation != orientation_rank1)

foreach y in win_indiv_next votesh_indiv_R1_next qualif_indiv_next{

eststo: bootstrap upper_bound=e(upper_bound) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("`y'") running_level_next("running_indiv_next") band(0.025)
sum `y' if running>=-0.025 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)
}

use "$output/database_runoff", clear

keep if main_sample_orient3==1 & assignment_type==3

gen or2 = orientation if ranking_indiv_R1 == 2
gen or3 = orientation if ranking_indiv_R1 == 3
keep if (secround_type == 1 & or3 != orientation_rank1 & or3 != orientation_rank2) | (secround_type == 2 & orientation != orientation_rank1)

eststo: bootstrap upper_bound=e(upper_bound2) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next_alt, outcome_next("nb_othercands_orient_R1_next") running_level_next("running_indiv_next") band(0.025)
estadd scalar band=0.025
sum nb_othercands_orient_R1_next if running>=-0.025 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableA13b.tex", replace b(3) se noobs nomtitles nonotes booktabs star(* 0.10 ** 0.05 *** 0.01) ///
coeflabels(upper_bound "Upper bound" lower_bound "Lower bound") mgroups("\shortstack{Cand. wins,\\t+1}" "\shortstack{Cand. vote\\share, t+1}" "\shortstack{Cand. qualif.,\\t+1}" "\shortstack{Orient. nb. other \\cand.,t+1}", pattern(1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(N_reps mean_control, fmt(0 3) label( "Bootstrap replications" "Mean, left of threshold"))

// Table A.14
estimates clear

use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3

eststo: bootstrap upper_bound=e(upper_bound) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("totexp_indiv_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.025
sum totexp_indiv_next if running>=-0.025 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)

use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3

eststo: bootstrap upper_bound=e(upper_bound) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("totcontrib_indiv_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.025
sum totcontrib_indiv_next if running>=-0.025 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)	

use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3	

//standardize originality index for interpretation
egen temp=sd(originality_indiv_next)
replace originality_indiv_next=originality_indiv_next/temp
drop temp

eststo: bootstrap upper_bound=e(bound_10pct) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("originality_indiv_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.025
sum originality_indiv_next if running>=-0.025 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)

use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3

eststo: bootstrap upper_bound=e(bound_10pct) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("sh_perso_indiv_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.025
sum sh_perso_indiv_next if running>=-0.025 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)

use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3

eststo: bootstrap upper_bound=e(bound_10pct) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("sh_vpp_indiv_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.025
sum sh_vpp_indiv_next if running>=-0.025 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableA14.tex", replace b(3) se noobs nomtitles nonotes booktabs star(* 0.10 ** 0.05 *** 0.01) ///
coeflabels(upper_bound "Upper bound" lower_bound "Lower bound") mgroups("\shortstack{Expenditures,\\ t+1}" "\shortstack{Contributions,\\ t+1}" "\shortstack{Manifesto originality,\\ t+1}" "\shortstack{First person,\\ t+1}" "\shortstack{Past tense,\\ t+1}", pattern(1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(N_reps mean_control, fmt(0 3) label( "Bootstrap replications" "Mean, left of threshold"))

***** Appendix B *****

// Figure B.1
foreach level in "indiv" "party" "orient"{

use "$output/database_win", clear

keep if main_sample_`level'==1 //main sample for each level of analysis

rddensity running, c(0) all plot plot_range(-0.1 0.1) bwselect(comb) fitselect(unrestricted) ///
graph_opt(xtitle(Running variable) ytitle(Density) graphregion(color(white)) legend(off))
graph export "$appendix/FigureB1_`level'.png", replace	

}

// Figure B.2
use "$output/database_win", clear

keep if placebo_sample_indiv==1

rdplot win_indiv_prev running, p(2) nbins(20 20) ///
graph_options(ytitle("Cand. wins, t-1") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$appendix/FigureB2_win_indiv.png", replace						

rdplot running_indiv_prev running, p(2) nbins(20 20) ///
graph_options(ytitle("Cand. runs, t-1") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$appendix/FigureB2_run_indiv.png", replace	

use "$output/database_win", clear

keep if placebo_sample_party==1

rdplot win_party_prev running, p(2) nbins(20 20) ///
graph_options(ytitle("Party wins, t-1") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$appendix/FigureB2_win_party.png", replace	
		
rdplot running_party_prev running, p(2) nbins(20 20) ///
graph_options(ytitle("Party runs, t-1") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$appendix/FigureB2_run_party.png", replace	

use "$output/database_win", clear

keep if placebo_sample_orient==1

rdplot win_orient_prev running, p(2) nbins(20 20) ///
graph_options(ytitle("Orient. wins, t-1") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$appendix/FigureB2_win_orient.png", replace

rdplot nb_othercands_orient_R1_prev running, p(2) nbins(20 20) ///
graph_options(ytitle("Orient. nb. other cand., t-1") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$appendix/FigureB2_orient.png", replace	

use "$output/database_win", clear

keep if main_sample_orient==1

rdplot nb_othercands_orient_R1 running, p(2) nbins(20 20) ///
graph_options(ytitle("Orient. nb. other cand., t") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$appendix/FigureB2_orient_2.png", replace	

// Figure B.3
foreach level in "indiv" "party" "orient"{

use "$output/database_runoff", clear

keep if assignment_type==3

keep if main_sample_`level'3==1 

rddensity running, c(0) all plot plot_range(-0.1 0.1) bwselect(comb) fitselect(unrestricted) ///
graph_opt(xtitle(Running variable) ytitle(Density) graphregion(color(white)) legend(off))
graph export "$appendix/FigureB3_`level'.png", replace	

}

// Figure B.4
foreach level in "indiv" "party" "orient"{

use "$output/database_runoff", clear

keep if assignment_type==3

keep if main_sample_`level'3==1 

rdplot treat_pred_`level' running if running<=0.125, p(2) nbins(12 12) ///
graph_options(xtitle(Running variable) ytitle(Predicted treatment status) graphregion(color(white)) legend(off))
graph export "$appendix/FigureB4_`level'.png", replace	

}

// Table B.1
estimates clear

use "$output/database_win", clear

keep if main_sample_indiv==1

foreach y in dummy_fl dummy_l dummy_c dummy_r dummy_fr nb_othercands_orient_R1 dummy_nc realparty_cand gender{

eststo: rdrobust `y' running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

}
		
esttab using "$appendix/TableB1a.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("\shortstack{Far-left}" "\shortstack{Left}" "\shortstack{Center}" "\shortstack{Right}" "\shortstack{Far-right}" "\shortstack{Non\\classified}" "\shortstack{Orient. nb.\\other cand.}" "\shortstack{Party cand.\\vs. ind.}" "\shortstack{Female\\cand.}" , pattern(1 1 1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
 
estimates clear

use "$output/database_win", clear

keep if placebo_sample_indiv==1

foreach y in win_indiv_prev votesh_indiv_R1_prev running_indiv_prev win_party_prev votesh_party_R1_prev running_party_prev win_orient_prev votesh_orient_R1_prev running_party_prev nb_othercands_orient_R1_prev{

eststo: rdrobust `y' running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

}

esttab using "$appendix/TableB1b.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("\shortstack{Cand. wins,\\t-1}" "\shortstack{Cand. vote\\share, t-1}" "\shortstack{Cand. runs,\\t-1}" "\shortstack{Party wins,\\t-1}" "\shortstack{Party vote\\share, t-1}" "\shortstack{Party runs,\\t-1}" "\shortstack{Orient. wins,\\t-1}" "\shortstack{Orient. vote\\share, t-1}" "\shortstack{Orient. runs,\\t-1}" "\shortstack{Orient. nb. other\\cand., t-1}", pattern(1 1 1 1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

// Table B.2
estimates clear

foreach level in "indiv" "party" "orient"{

use "$output/database_runoff", clear

keep if assignment_type==3

keep if main_sample_`level'3==1

eststo: rdrobust treat_pred_`level' running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum treat_pred_`level' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

esttab using "$appendix/TableB2.tex", b(3) se nonotes booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("Predicted treatment status", pattern(1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles("Cand." "Party"  "Orient." )

***** Appendix C *****
 
// Figure C.1
mata: mata clear
clear

local bw_min	0.02
local bw_max 	0.2
local bw_gap 	0.01
local n_cols 	4
local col_bw 	1
local col_tau 	2
local col_CI_l	3
local col_CI_r 	4
mat results = J(1, `n_cols', 0)
mat colnames results = "bw tau CI_l CI_r"
mat row = J(1, `n_cols', 0)

use "$output/database_win", clear

keep if main_sample_indiv==1	

rdrobust win_indiv_next running, h(0.01) vce(nncluster code_district)
local bw_initial = e(h_l)
mat results[1, `col_bw'] 	= `bw_initial'
mat results[1, `col_tau'] 	= e(tau_cl)
mat results[1, `col_CI_l']  = e(ci_l_rb)
mat results[1, `col_CI_r']	= e(ci_r_rb)
forvalues bw = `bw_min'(`bw_gap')`bw_max' {
rdrobust win_indiv_next running, h(`bw') vce(nncluster code_district)
mat row[1, `col_bw'] 	= `bw'
mat row[1, `col_tau'] 	= e(tau_cl)
mat row[1, `col_CI_l']	= e(ci_l_rb)
mat row[1, `col_CI_r']	= e(ci_r_rb)
mat results = results \ row
}

clear

use "$output/database_win"

keep if main_sample_indiv==1	

quietly rdrobust win_indiv_next running, vce(nncluster code_district)
local bdw_CCT=e(h_l)
rdob win_indiv_next running
local bdw_IK=.11368522
svmat results, names(col)
twoway	(scatter CI_l bw, lcolor(gs8) lpattern(shortdash) recast(line)) ///
	(scatter CI_r bw, lcolor(gs8) lpattern(shortdash) recast(line)) ///
	(scatter tau bw, mcolor(gs6) sort(bw)), xline(0.05, lcolor(red)) xline(`bdw_CCT', lcolor(green)) xline(`bdw_IK', lcolor(blue)) ///
	xlabel(.01(.02).2) ylabel(-.1 (.1) .4) ytitle("Treatment effect - Cand. wins, t+1") xtitle(Bandwidth) ///
	graphregion(fcolor(white) lcolor(white) margin(zero)) legend(off)
graph export "$appendix/FigureC1_indiv.png", replace
mata: mata clear

clear
local bw_min	0.02
local bw_max 	0.2
local bw_gap 	0.01
local n_cols 	4
local col_bw 	1
local col_tau 	2
local col_CI_l	3
local col_CI_r 	4
mat results = J(1, `n_cols', 0)
mat colnames results = "bw tau CI_l CI_r"
mat row = J(1, `n_cols', 0)

use "$output/database_win", clear

keep if main_sample_party==1
	
rdrobust win_party_next running, h(0.01) vce(nncluster code_district)
local bw_initial = e(h_l)
mat results[1, `col_bw'] 	= `bw_initial'
mat results[1, `col_tau'] 	= e(tau_cl)
mat results[1, `col_CI_l']  = e(ci_l_rb)
mat results[1, `col_CI_r']	= e(ci_r_rb)
forvalues bw = `bw_min'(`bw_gap')`bw_max' {
rdrobust win_party_next running, h(`bw') vce(nncluster code_district)
mat row[1, `col_bw'] 	= `bw'
mat row[1, `col_tau'] 	= e(tau_cl)
mat row[1, `col_CI_l']	= e(ci_l_rb)
mat row[1, `col_CI_r']	= e(ci_r_rb)
mat results = results \ row
}

clear

use "$output/database_win"

keep if main_sample_party==1
	
quietly rdrobust win_party_next running, vce(nncluster code_district)
local bdw_CCT=e(h_l)
rdob win_party_next running
local bdw_IK=.15278321
svmat results, names(col)
twoway	(scatter CI_l bw, lcolor(gs8) lpattern(shortdash) recast(line)) ///
	(scatter CI_r bw, lcolor(gs8) lpattern(shortdash) recast(line)) ///
	(scatter tau bw, mcolor(gs6) sort(bw)), xline(0.05, lcolor(red)) xline(`bdw_CCT', lcolor(green)) xline(`bdw_IK', lcolor(blue)) ///
	xlabel(.01(.02).2) ylabel(-.1 (.1) .4) ytitle("Treatment effect - Party. wins, t+1") xtitle(Bandwidth) ///
	graphregion(fcolor(white) lcolor(white) margin(zero)) legend(off)
graph export "$appendix/FigureC1_party.png", replace
mata: mata clear

clear
local bw_min	0.02
local bw_max 	0.2
local bw_gap 	0.01
local n_cols 	4
local col_bw 	1
local col_tau 	2
local col_CI_l	3
local col_CI_r 	4
mat results = J(1, `n_cols', 0)
mat colnames results = "bw tau CI_l CI_r"
mat row = J(1, `n_cols', 0)

use "$output/database_win", clear

keep if main_sample_orient==1	

rdrobust win_orient_next running, h(0.01) vce(nncluster code_district)
local bw_initial = e(h_l)
mat results[1, `col_bw'] 	= `bw_initial'
mat results[1, `col_tau'] 	= e(tau_cl)
mat results[1, `col_CI_l']  = e(ci_l_rb)
mat results[1, `col_CI_r']	= e(ci_r_rb)
forvalues bw = `bw_min'(`bw_gap')`bw_max' {
rdrobust win_orient_next running, h(`bw') vce(nncluster code_district)
mat row[1, `col_bw'] 	= `bw'
mat row[1, `col_tau'] 	= e(tau_cl)
mat row[1, `col_CI_l']	= e(ci_l_rb)
mat row[1, `col_CI_r']	= e(ci_r_rb)
mat results = results \ row
}

clear

use "$output/database_win"

keep if main_sample_orient==1

quietly rdrobust win_orient_next running, vce(nncluster code_district)
local bdw_CCT=e(h_l)
rdob win_orient_next running
local bdw_IK=.11964646
svmat results, names(col)
twoway	(scatter CI_l bw, lcolor(gs8) lpattern(shortdash) recast(line)) ///
	(scatter CI_r bw, lcolor(gs8) lpattern(shortdash) recast(line)) ///
	(scatter tau bw, mcolor(gs6) sort(bw)), xline(0.05, lcolor(red)) xline(`bdw_CCT', lcolor(green)) xline(`bdw_IK', lcolor(blue)) ///
	xlabel(.01(.02).2) ylabel(-.1 (.1) .4) ytitle("Treatment effect - Orient. wins, t+1") xtitle(Bandwidth) ///
	graphregion(fcolor(white) lcolor(white) margin(zero)) legend(off)
graph export "$appendix/FigureC1_orient.png", replace
mata: mata clear
			
// Figure C.2
mata: mata clear

clear
local bw_min	0.02
local bw_max 	0.2
local bw_gap 	0.01
local n_cols 	4
local col_bw 	1
local col_tau 	2
local col_CI_l	3
local col_CI_r 	4
mat results = J(1, `n_cols', 0)
mat colnames results = "bw tau CI_l CI_r"
mat row = J(1, `n_cols', 0)

use "$output/database_win", clear

keep if main_sample_indiv==1
	
rdrobust treat_pred_indiv running, h(0.01) vce(nncluster code_district)
local bw_initial = e(h_l)
mat results[1, `col_bw'] 	= `bw_initial'
mat results[1, `col_tau'] 	= e(tau_cl)
mat results[1, `col_CI_l']  = e(ci_l_rb)
mat results[1, `col_CI_r']	= e(ci_r_rb)
forvalues bw = `bw_min'(`bw_gap')`bw_max' {
	rdrobust treat_pred_indiv running, h(`bw') vce(nncluster code_district)
	mat row[1, `col_bw'] 	= `bw'
	mat row[1, `col_tau'] 	= e(tau_cl)
	mat row[1, `col_CI_l']	= e(ci_l_rb)
	mat row[1, `col_CI_r']	= e(ci_r_rb)
	mat results = results \ row
}

clear

use "$output/database_win"

keep if main_sample_indiv==1	

quietly rdrobust treat_pred_indiv running, vce(nncluster code_district)
local bdw_CCT=e(h_l)
rdob treat_pred_indiv running
local bdw_IK=.17158009
svmat results, names(col)
twoway	(scatter CI_l bw, lcolor(gs8) lpattern(shortdash) recast(line)) ///
		(scatter CI_r bw, lcolor(gs8) lpattern(shortdash) recast(line)) ///
		(scatter tau bw, mcolor(gs6) sort(bw)), xline(0.05, lcolor(red)) xline(`bdw_CCT', lcolor(green)) xline(`bdw_IK', lcolor(blue)) ///
		xlabel(.01(.02).2) ylabel(-.3 (.1) .2) ytitle("Treatment effect - Predicted treatment status") xtitle(Bandwidth) ///
		graphregion(fcolor(white) lcolor(white) margin(zero)) legend(off)
graph export "$appendix/FigureC2_indiv.png", replace
mata: mata clear

clear
local bw_min	0.02
local bw_max 	0.2
local bw_gap 	0.01
local n_cols 	4
local col_bw 	1
local col_tau 	2
local col_CI_l	3
local col_CI_r 	4
mat results = J(1, `n_cols', 0)
mat colnames results = "bw tau CI_l CI_r"
mat row = J(1, `n_cols', 0)

use "$output/database_win", clear

keep if main_sample_party==1	

rdrobust treat_pred_party running, h(0.01) vce(nncluster code_district)
local bw_initial = e(h_l)
mat results[1, `col_bw'] 	= `bw_initial'
mat results[1, `col_tau'] 	= e(tau_cl)
mat results[1, `col_CI_l']  = e(ci_l_rb)
mat results[1, `col_CI_r']	= e(ci_r_rb)
forvalues bw = `bw_min'(`bw_gap')`bw_max' {
	rdrobust treat_pred_party running, h(`bw') vce(nncluster code_district)
	mat row[1, `col_bw'] 	= `bw'
	mat row[1, `col_tau'] 	= e(tau_cl)
	mat row[1, `col_CI_l']	= e(ci_l_rb)
	mat row[1, `col_CI_r']	= e(ci_r_rb)
	mat results = results \ row
}

clear

use "$output/database_win"

keep if main_sample_party==1	

quietly rdrobust treat_pred_party running, vce(nncluster code_district)
local bdw_CCT=e(h_l)
rdob treat_pred_party running
local bdw_IK= .14629779
svmat results, names(col)
twoway	(scatter CI_l bw, lcolor(gs8) lpattern(shortdash) recast(line)) ///
		(scatter CI_r bw, lcolor(gs8) lpattern(shortdash) recast(line)) ///
		(scatter tau bw, mcolor(gs6) sort(bw)), xline(0.05, lcolor(red)) xline(`bdw_CCT', lcolor(green)) xline(`bdw_IK', lcolor(blue)) ///
		xlabel(.01(.02).2) ylabel(-.3 (.1) .2) ytitle("Treatment effect - Predicted treatment status") xtitle(Bandwidth) ///
		graphregion(fcolor(white) lcolor(white) margin(zero)) legend(off)
graph export "$appendix/FigureC2_party.png", replace
mata: mata clear

clear
local bw_min	0.02
local bw_max 	0.2
local bw_gap 	0.01
local n_cols 	4
local col_bw 	1
local col_tau 	2
local col_CI_l	3
local col_CI_r 	4
mat results = J(1, `n_cols', 0)
mat colnames results = "bw tau CI_l CI_r"
mat row = J(1, `n_cols', 0)

use "$output/database_win", clear

keep if main_sample_orient==1	

rdrobust treat_pred_orient running, h(0.01) vce(nncluster code_district)
local bw_initial = e(h_l)
mat results[1, `col_bw'] 	= `bw_initial'
mat results[1, `col_tau'] 	= e(tau_cl)
mat results[1, `col_CI_l']  = e(ci_l_rb)
mat results[1, `col_CI_r']	= e(ci_r_rb)
forvalues bw = `bw_min'(`bw_gap')`bw_max' {
	rdrobust treat_pred_orient running, h(`bw') vce(nncluster code_district)
	mat row[1, `col_bw'] 	= `bw'
	mat row[1, `col_tau'] 	= e(tau_cl)
	mat row[1, `col_CI_l']	= e(ci_l_rb)
	mat row[1, `col_CI_r']	= e(ci_r_rb)
	mat results = results \ row
}

clear

use "$output/database_win"

keep if main_sample_orient==1

quietly rdrobust treat_pred_orient running, vce(nncluster code_district)
local bdw_CCT=e(h_l)
rdob treat_pred_orient running
local bdw_IK=.18218055
svmat results, names(col)
twoway	(scatter CI_l bw, lcolor(gs8) lpattern(shortdash) recast(line)) ///
		(scatter CI_r bw, lcolor(gs8) lpattern(shortdash) recast(line)) ///
		(scatter tau bw, mcolor(gs6) sort(bw)), xline(0.05, lcolor(red)) xline(`bdw_CCT', lcolor(green)) xline(`bdw_IK', lcolor(blue)) ///
		xlabel (.01(.02).2) ylabel(-.3 (.1) .2) ytitle("Treatment effect - Predicted treatment status") xtitle(Bandwidth) ///
		graphregion(fcolor(white) lcolor(white) margin(zero)) legend(off)
graph export "$appendix/FigureC2_orient.png", replace
mata: mata clear

// Table C.1
estimates clear

use "$output/database_win", clear

keep if main_sample_indiv==1

eststo: rdrobust win_indiv_next running, vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

rdob win_indiv_next running
eststo: rdrobust win_indiv_next running, h(.11292509) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
		
eststo: rdrobust win_indiv_next running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust win_indiv_next running, h(0.1) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust win_indiv_next running, h(0.05) p(2) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableC1a.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("\shortstack{Cand. wins, t+1}", pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

estimates clear 

use "$output/database_win", clear

keep if main_sample_party==1

eststo: rdrobust win_party_next running, vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_party_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

rdob win_party_next running
eststo: rdrobust win_party_next running, h(.15278321) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_party_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust win_party_next running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_party_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust win_party_next running, h(0.1) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_party_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust win_party_next running, h(0.05) p(2) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_party_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableC1b.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
mgroups("\shortstack{Party wins, t+1}", pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

estimates clear 

use "$output/database_win", clear

keep if main_sample_orient==1

eststo: rdrobust win_orient_next running, vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_orient_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

rdob win_orient_next running
eststo: rdrobust win_orient_next running, h(.11964646) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_orient_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust win_orient_next running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_orient_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust win_orient_next running, h(0.1) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_orient_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust win_orient_next running, h(0.05) p(2) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_orient_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableC1c.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
mgroups("\shortstack{Orient. wins, t+1}", pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

// Table C.2
estimates clear

foreach level in "indiv" "party" "orient"{

use "$output/database_win", clear

keep if main_sample_`level'==1
		
eststo: rdrobust win_`level'_next running, h(0.05) covs(dummy_fl dummy_l dummy_nc dummy_r dummy_fr) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_`level'_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
		}
		
esttab using "$appendix/TableC2.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("\shortstack{Cand. wins,\\t+1}" "\shortstack{Party wins,\\t+1}" "\shortstack{Orient. wins,\\t+1}", pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
 
// Table C.3
estimates clear

use "$output/database_win", clear

keep if main_sample_indiv==1

eststo: rdrobust running_indiv_next running, vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

rdob running_indiv_next running
eststo: rdrobust running_indiv_next running, h(.12581729) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust running_indiv_next running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust running_indiv_next running, h(0.1) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust running_indiv_next running, p(2) h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
	
esttab using "$appendix/TableC3a.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
mgroups("\shortstack{Cand. runs, t+1}", pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

estimates clear

use "$output/database_win", clear

keep if main_sample_party==1

eststo: rdrobust running_party_next running, vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_party_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

rdob running_party_next running
eststo: rdrobust running_party_next running, h(.10743167) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_party_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust running_party_next running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_party_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)	
	
eststo: rdrobust running_party_next running, h(0.1) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_party_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust running_party_next running, p(2) h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_party_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableC3b.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
mgroups("\shortstack{Party runs, t+1}", pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

estimates clear

use "$output/database_win", clear

keep if main_sample_orient==1

eststo: rdrobust nb_othercands_orient_R1_next running, vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_othercands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

rdob nb_othercands_orient_R1_next running
eststo: rdrobust nb_othercands_orient_R1_next running, h(.19334469) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_othercands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust nb_othercands_orient_R1_next running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_othercands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust nb_othercands_orient_R1_next running, h(0.1) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_othercands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust nb_othercands_orient_R1_next running, p(2) h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_othercands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableC3c.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
mgroups("\shortstack{Orient nb. other cand., t+1}", pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

estimates clear

use "$output/database_win", clear

keep if main_sample_orient==1

eststo: rdrobust nb_cands_orient_R1_next running, vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_cands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

rdob nb_cands_orient_R1_next running
eststo: rdrobust nb_cands_orient_R1_next running, h(.1889889) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_cands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust nb_cands_orient_R1_next running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_cands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust nb_cands_orient_R1_next running, h(0.1) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_cands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust nb_cands_orient_R1_next running, p(2) h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_cands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

esttab using "$appendix/TableC3d.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
mgroups("\shortstack{Orient nb. all cand., t+1}", pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

***** Appendix D *****

// Table D.1
tempfile temp 

use "$output/database_win", clear

keep if main_sample_indiv==1 

gen flag_baseline_h = cond(abs(running)<=0.05,1,0) 

bysort id_unique: gen duplicates = cond(_N==1,0,_n)
drop if duplicates>1
drop duplicates
bysort election_type year year_next: gen num_elections=_N 
bysort election_type year year_next: egen num_effective_elections = total(flag_baseline_h)
bysort election_type year year_next: gen duplicates = cond(_N==1,0,_n)
drop if duplicates>1
drop duplicates

keep election_type year year_next num_elections num_effective_elections
sort election_type year year_next 
order election_type year year_next num_elections num_effective_elections
save `temp', replace 

keep if election_type==1
egen total_num_elections = total(num_elections)
egen total_num_effective_elections = total(num_effective_elections)

set obs `=_N+1'
replace year=0 if year==.
set obs `=_N+1'
replace year=1 if year==.
set obs `=_N+1'
replace year=2 if year==.

sort year
gen eltype=""
replace eltype="Election type" if year==0
tostring eltype, replace
replace eltype = "parliamentary elections" if !missing(election_type)

tostring year, replace
replace year="Year t" if year=="0"
replace year="(running variable)" if year=="1"
replace year="(1)" if year=="2"

tostring year_next, replace
replace year_next="Year t+1" if year=="Year t"
replace year_next="(electoral outcomes)" if year=="(running variable)"
replace year_next="(2)" if year=="(1)"

tostring num_elections, replace
replace num_elections="Nb of races" if year=="Year t"
replace num_elections="(3)" if year=="(1)"
replace num_elections="" if num_elections=="."

tostring num_effective_elections, replace
replace num_effective_elections="Nb of effective races" if year=="Year t"
replace num_effective_elections="(4)" if year=="(1)"
replace num_effective_elections="" if num_effective_elections=="."

drop election_type 
order eltype year year_next num_elections num_effective_elections
gen row_id = _n
replace eltype = "" if row_id>3
set obs `=_N+1'
replace eltype = "Total" if missing(row_id)

replace total_num_elections=total_num_elections[_n-1] if missing(total_num_elections)
replace total_num_effective_elections=total_num_effective_elections[_n-1] if missing(total_num_effective_elections)
tostring total_num_elections, replace
tostring total_num_effective_elections, replace
replace num_elections=total_num_elections if missing(row_id)
replace num_effective_elections=total_num_effective_elections if missing(row_id)
drop total_num_elections total_num_effective_elections row_id
order eltype year year_next num_elections num_effective_elections
drop num_effective_elections

texsave using "$appendix/TableD1.tex" , hlines(3 15) align(lccc) frag nonames  replace

// Table D.2
use `temp', clear

keep if election_type==2

egen total_num_elections = total(num_elections)
egen total_num_effective_elections = total(num_effective_elections)

set obs `=_N+1'
replace year=0 if year==.
set obs `=_N+1'
replace year=1 if year==.
set obs `=_N+1'
replace year=2 if year==.

sort year year_next
gen eltype=""
replace eltype="Election type" if year==0
tostring eltype, replace
replace eltype = "local elections" if !missing(election_type)

tostring year, replace
replace year="Year t" if year=="0"
replace year="(running variable)" if year=="1"
replace year="(1)" if year=="2"

tostring year_next, replace
replace year_next="Year t+1" if year=="Year t"
replace year_next="(electoral outcomes)" if year_next=="."
replace year_next="(2)" if year=="(1)"

tostring num_elections, replace
replace num_elections="Nb of races" if year=="Year t"
replace num_elections="(3)" if year=="(1)"
replace num_elections="" if num_elections=="."

tostring num_effective_elections, replace
replace num_effective_elections="Nb of effective races" if year=="Year t"
replace num_effective_elections="(4)" if year=="(1)"
replace num_effective_elections="" if num_effective_elections=="."

drop election_type 
order eltype year year_next num_elections num_effective_elections
gen row_id = _n
replace eltype = "" if row_id>3
set obs `=_N+1'
replace eltype = "Total" if missing(row_id)

replace total_num_elections=total_num_elections[_n-1] if missing(total_num_elections)
replace total_num_effective_elections=total_num_effective_elections[_n-1] if missing(total_num_effective_elections)
tostring total_num_elections, replace
tostring total_num_effective_elections, replace
replace num_elections=total_num_elections if missing(row_id)
replace num_effective_elections=total_num_effective_elections if missing(row_id)
drop total_num_elections total_num_effective_elections row_id
order eltype year year_next num_elections num_effective_elections
drop num_effective_elections

texsave using "$appendix/TableD2.tex" , hlines(3 22) align(lccc)  frag nonames  replace
	
// Table D.3
estimates clear

use "$output/database_win", clear

keep if main_sample_indiv==1

byso id_unique: keep if _n==1
gen parl=election_type==1
estpost summarize nb_registered_R1 turnout_R1 nb_cands_R1 secround_district margin	
esttab  using "$appendix/TableD3.tex", ///
replace nofloat nonumber noobs label style(tex) ///
cells(( mean(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Mean")) ///
sd(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Sd")) ///
min(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Min")) /// 
max(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Max")) ///
count(fmt(%15.0fc) label("Obs."))))  ///
refcat(nb_registered_R1 "\textbf{A. Candidate-level}", nolabel) ///
coeflabels(nb_registered_R1 "Registered voters" turnout_R1 "Turnout" nb_cands_R1 "Nb. of cand." secround_district "Runoff" margin "Winning margin") ///
postfoot("") 

use "$output/database_win", clear

keep if main_sample_party==1

byso id_unique: keep if _n==1
gen parl=election_type==1
estpost summarize nb_registered_R1 turnout_R1 nb_cands_R1 secround_district margin
esttab  using "$appendix/TableD3.tex", ///
append  label style(tex) mlabels(none) mtitles(none) collabels(none) noobs nonumber ///
cells(( mean(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Mean")) ///
sd(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Sd")) ///
min(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Min")) /// 
max(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Max")) ///
count(fmt(%15.0fc) label("Obs."))))  ///
refcat(nb_registered_R1 "\textbf{B. Party-level}", nolabel) ///
coeflabels(nb_registered_R1 "Registered voters" turnout_R1 "Turnout" nb_cands_R1 "Nb. of cand." secround_district "Runoff" margin "Winning margin") ///
prehead("")   posthead("")		///
postfoot("") 			

use "$output/database_win", clear

keep if main_sample_orient==1

byso id_unique: keep if _n==1
gen parl=election_type==1
estpost summarize nb_registered_R1 turnout_R1 nb_cands_R1 secround_district margin
esttab  using "$appendix/TableD3.tex", ///
append  label style(tex) mlabels(none) mtitles(none) collabels(none) noobs nonumber ///
cells(( mean(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Mean")) ///
sd(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Sd")) ///
min(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Min")) /// 
max(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Max")) ///
count(fmt(%15.0fc) label("Obs."))))  ///
refcat(nb_registered_R1 "\textbf{C. Orientation-level}", nolabel) ///
coeflabels(nb_registered_R1 "Registered voters" turnout_R1 "Turnout" nb_cands_R1 "Nb. of cand." secround_district "Runoff" margin "Winning margin") ///
prehead("")  posthead("") 

// Table D.4 
estimates clear

use "$output/database_win", clear

keep if main_sample_indiv==1 & abs(running)<=0.05

byso id_unique: keep if _n==1
gen parl=election_type==1
estpost summarize nb_registered_R1 turnout_R1 nb_cands_R1 secround_district margin
esttab  using "$appendix/TableD4.tex", ///
replace nofloat nonumber noobs label style(tex) ///
cells(( mean(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Mean")) ///
sd(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Sd")) ///
min(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Min")) /// 
max(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Max")) ///
count(fmt(%15.0fc) label("Obs."))))  ///
refcat(nb_registered_R1 "\textbf{A. Candidate-level}", nolabel) ///
coeflabels(nb_registered_R1 "Registered voters" turnout_R1 "Turnout" nb_cands_R1 "Nb. of cand." secround_district "Runoff" margin "Winning margin") ///
postfoot("") 

use "$output/database_win", clear

keep if main_sample_party==1  & abs(running)<=0.05

byso id_unique: keep if _n==1
gen parl=election_type==1
estpost summarize nb_registered_R1 turnout_R1 nb_cands_R1 secround_district margin
esttab  using "$appendix/TableD4.tex", ///
append  label style(tex) mlabels(none) mtitles(none) collabels(none) noobs nonumber ///
cells(( mean(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Mean")) ///
sd(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Sd")) ///
min(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Min")) /// 
max(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Max")) ///
count(fmt(%15.0fc) label("Obs."))))  ///
refcat(nb_registered_R1 "\textbf{B. Party-level}", nolabel) ///
coeflabels(nb_registered_R1 "Registered voters" turnout_R1 "Turnout" nb_cands_R1 "Nb. of cand." secround_district "Runoff" margin "Winning margin") ///
prehead("")   posthead("")		///
postfoot("") 

use "$output/database_win", clear
	
keep if main_sample_orient==1 & abs(running)<=0.05	

byso id_unique: keep if _n==1
gen parl=election_type==1	
estpost summarize nb_registered_R1 turnout_R1 nb_cands_R1 secround_district margin
esttab  using "$appendix/TableD4.tex", ///
append  label style(tex) mlabels(none) mtitles(none) collabels(none) noobs nonumber ///
cells(( mean(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Mean")) ///
sd(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Sd")) ///
min(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Min")) /// 
max(fmt(%15.1fc %15.3fc  %15.1fc %15.3fc %15.3fc) label("Max")) ///
count(fmt(%15.0fc) label("Obs."))))  ///
refcat(nb_registered_R1 "\textbf{C. Orientation-level}", nolabel) ///
coeflabels(nb_registered_R1 "Registered voters" turnout_R1 "Turnout" nb_cands_R1 "Nb. of cand." secround_district "Runoff" margin "Winning margin") ///
prehead("")  posthead("") 

